package com.qiangesoft.wechat.config;

import com.qiangesoft.wechat.core.handler.LogHandler;
import com.qiangesoft.wechat.core.handler.event.SubscribeHandler;
import com.qiangesoft.wechat.core.handler.event.UnsubscribeHandler;
import com.qiangesoft.wechat.core.handler.message.ImageHandler;
import com.qiangesoft.wechat.core.handler.message.LocationHandler;
import com.qiangesoft.wechat.core.handler.message.MessageHandler;
import com.qiangesoft.wechat.core.handler.message.VoiceHandler;
import com.qiangesoft.wechat.core.interceptor.MessageInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信公众号配置
 *
 * @author qiangesoft
 * @date 2023-09-07
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class WxMpConfiguration {

    /**
     * 微信公众号配置信息
     */
    private final WxMpProperties wxMpProperties;
    /**
     * 日志处理器
     */
    private final LogHandler logHandler;

    /**
     * 订阅事件处理器
     */
    private final SubscribeHandler subscribeHandler;
    /**
     * 取消订阅事件处理器
     */
    private final UnsubscribeHandler unsubscribeHandler;

    /**
     * 语音消息处理器
     */
    private final VoiceHandler voiceHandler;
    /**
     * 文本消息处理器
     */
    private final MessageHandler messageHandler;
    /**
     * 图片消息处理器
     */
    private final ImageHandler imageHandler;
    /**
     * 位置消息处理器
     */
    private final LocationHandler locationHandler;

    /**
     * 消息拦截器
     */
    private final MessageInterceptor messageInterceptor;

    /**
     * 声明实例
     *
     * @return
     */
    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    /**
     * 配置存储方式
     *
     * @return
     */
    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        // 公众号appId
        configStorage.setAppId(wxMpProperties.getAppId());
        // 公众号appSecret
        configStorage.setSecret(wxMpProperties.getAppSecret());
        // 公众号Token
        configStorage.setToken(wxMpProperties.getToken());
        // 公众号EncodingAESKey
        configStorage.setAesKey(wxMpProperties.getAesKey());
        return configStorage;
    }

    /**
     * 配置公众号的事件路由
     * <p>
     * 1.用户发消息
     * 2.关注事件
     * 3.取消关注事件
     * ......
     */
    @Bean
    public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
        WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
        // 记录所有消息事件的日志
        newRouter.rule().async(false).handler(this.logHandler).next();


        // 文本消息
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.TEXT).interceptor(this.messageInterceptor).handler(this.messageHandler).end();

        // 语音消息
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.VOICE).handler(this.voiceHandler).end();

        // 图片消息
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.IMAGE).handler(this.imageHandler).end();

        // 位置消息
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.LOCATION).handler(this.locationHandler).end();


        // 关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE).handler(this.subscribeHandler).end();

        // 取消关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.UNSUBSCRIBE).handler(this.unsubscribeHandler).end();

        return newRouter;
    }
}
